package com.example.storm.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MusicService extends Service {
    public MusicService() {
    }
    private int progress=0;
    private boolean isPaused = false;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int action = intent.getIntExtra("action",-1);
        switch (action){
            case 1:
                play();
                break;
            case 2:
                pause();
                break;
            case 3:
                resume();
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new MyBinder();
    }
    public class MyBinder extends Binder{
        public int getProgress(){
            return progress;
        }
    }
    public void play(){
        new Thread(){
            @Override
            public void run() {
                synchronized (MusicService.this){
                if(isPaused){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for(int i =0;i<=800;i++) {
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progress++;
                }
                }
            }
        }.start();

    }
    public synchronized void pause(){
        isPaused = true;

    }
    public synchronized void resume(){
        isPaused = false;
        notify();
    }
}
